<?php

header("Content-Type: text/html; charset=utf-8");

/**
 * MYSQL操作类
 * function:Open() 打开一个MYSQL连接,无返回值;
 * function:Close() 关闭一个已打开的MYSQL连接,无返回值;
 * function:Sql($sql) 执行一条SQL语句,
  @param $sql SQL语句,
  return 执行语句后的记录集;
 * function:Rows() 获得记录的总条数,
  return 记录的总条数;
 * function:Get($field,$row) 获取指定记录行和指定字段的值,
  @param $field 字段名称,
  @param $row 记录行数,
  return 当前记录字段的值;
 * function:Next_row() 移动到下一条记录,无返回值; 
 */
class Mysql_odb {

    private $mysql_server;
    private $mysql_db;
    private $mysql_user;
    private $mysql_pwd;
    private $mysql_charset;
    private $mysql_link;
    private $mysql_rs;
    private $mysql_rows;

    /**
     * MYSQL连接类构造函数
     * @param $mysql_server_var - MYSQL服务器名
     * @param $mysql_db_var - MYSQL数据库名
     * @param $mysql_user_var - MYSQL用户名
     * @param $mysql_pwd_var - MYSQL用户密码
     * @param $mysql_charset_var - MYSQL连接编码方式
     */
    function __construct() {
        global $mysql_server, $mysql_db, $mysql_user, $mysql_pwd, $mysql_charset;
        $this->mysql_server = $mysql_server;
        $this->mysql_db = $mysql_db;
        $this->mysql_user = $mysql_user;
        $this->mysql_pwd = $mysql_pwd;
        $this->mysql_charset = $mysql_charset;
    }

    public function Open() {
        $this->mysql_link = mysql_connect($this->mysql_server, $this->mysql_user, $this->mysql_pwd) or die("mysql error:" . mysql_error());
        mysql_query("set names '" . $this->mysql_charset . "';", $this->mysql_link);
    }

    public function Close() {
        mysql_close($this->mysql_link);
    }

    public function Sql($sql) {
        mysql_select_db($this->mysql_db, $this->mysql_link);
        $this->mysql_rs = mysql_query($sql, $this->mysql_link);
        return $this->mysql_rs;
    }

    public function Rows() {
        $this->mysql_rows = 0;
        if ($this->mysql_rs != 1 && $this->mysql_rs != 0) {
            $this->mysql_rows = mysql_num_rows($this->mysql_rs);
        }
        return $this->mysql_rows;
    }

    public function Get($field, $row) {
        $result = mysql_result($this_ > mysql_rs, $row, $field);
        return $result;
    }

    public function Next_row() {
        return "NULL";
    }

    function __destruct() {
        unset($this->mysql_server);
        unset($this->mysql_db);
        unset($this->mysql_user);
        unset($this->mysql_pwd);
        unset($this->mysql_link);
        unset($this->mysql_rs);
        unset($this->mysql_charset);
        unset($this->mysql_rows);
    }

}

/**
 * ACCESS操作类
 * function:Open() 打开一个ACCESS连接,无返回值;
 * function:Close() 关闭一个已打开的ACCESS连接,无返回值;
 * function:Sql($sql) 执行一条SQL语句,
  @param $sql SQL语句,
  return 执行语句后的记录集;
 * function:Rows() 获得记录的总条数,
  return 记录的总条数;
 * function:Get($field,$row) 获取指定记录行和指定字段的值,
  @param $field 字段名称,
  @param $row 记录行数,
  return 当前记录字段的值;
 * function:Next_row() 移动到下一条记录,无返回值; 
 */
class Access_odb {

    private $access_db;
    private $access_user;
    private $access_pwd;
    private $access_str;
    private $access_charset;
    private $access_link;
    private $access_rs;
    private $access_rows;

    /**
     * ACCESS连接类构造函数
     * @param $access_db_var - ACCESS数据库名
     * @param $access_user_var - ACCESS用户名
     * @param $access_pwd_var - ACCESS用户密码
     * @param $access_charset_var - ACCESS连接编码方式
     */
    function __construct() {
        global $access_db, $access_user, $access_pwd, $access_charset;
        $this->access_db = $access_db;
        $this->access_user = $access_user;
        $this->access_pwd = $access_pwd;
        $this->access_charset = $access_charset;
    }

    public function Open() {
//$this->access_str = "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=".realpath($this->access_db);
        $this->access_str = "DRIVER={Driver do Microsoft Access (*.mdb)};DBQ=" . realpath($this->access_db);
        $this->access_link = odbc_connect($this->access_str, $this->access_user, $this->access_pwd, SQL_CUR_USE_ODBC) or die("odbc error:" . odbc_error());
//$this->odbc_exec($this->access_link,"set names {$this->access_charset}"); 
    }

    public function Close() {
        odbc_close($this->access_link);
    }

    public function Sql($sql) {
        $re_sql = iconv($this->access_charset, "gb2312", $sql);
        $this->access_rs = odbc_exec($this->access_link, $re_sql);
        return $this->access_rs;
    }

    public function Rows() {
        $this->access_rows = 0;
        if ($this->access_rs != 1 && $this->access_rs != 0) {
            /**
             * $this->access_rows = odbc_num_rows($this->access_rs);
             * 因为ODBC驱动不支持,这段代码无法得到记录集的行数
             */
            ob_start();
            $this->access_rows = odbc_result_all($this->access_rs);
            ob_clean();
        }
        return $this->access_rows;
    }

    /**
     * get_result取结果集的值
     * @param $field - 要得到值的字段名或字段编号
     * @return 返回取得的值
     */
    public function Get($field, $row) {
//$rs = odbc_result($this->access_rs,$field);
        $list = odbc_fetch_array($this->access_rs, $row);
        $result = iconv("gb2312", $this->access_charset, $list[$field]);
        return $result;
    }

    public function Next_row() {
        odbc_fetch_row($this->access_rs);
    }

    function __destruct() {
        unset($this->access_db);
        unset($this->access_user);
        unset($this->access_pwd);
        unset($this->access_str);
        unset($this->access_link);
        unset($this->access_rs);
        unset($this->access_charset);
        unset($this->access_rows);
    }

}

/**
 * ACCESS操作类(COM方法)
 * function:Open() 打开一个ACCESS连接,无返回值;
 * function:Close() 关闭一个已打开的ACCESS连接,无返回值;
 * function:Sql($sql) 执行一条SQL语句,
  @param $sql SQL语句,
  return 执行语句后的记录集;
 * function:Rows() 获得记录的总条数,
  return 记录的总条数;
 * function:Get($field,$row) 获取指定记录行和指定字段的值,
  @param $field 字段名称,
  @param $row 记录行数,
  return 当前记录字段的值;
 * function:Next_row() 移动到下一条记录,无返回值; 
 */
class Access_codb {

    private $access_db;
    private $access_user;
    private $access_pwd;
    private $access_str;
    private $access_charset;
    private $access_link;
    private $access_rs;
    private $access_rows;
    private $access_array;
    private $field_count;

    /**
     * ACCESS连接类构造函数
     * @param $access_db_var - ACCESS数据库名
     * @param $access_user_var - ACCESS用户名
     * @param $access_pwd_var - ACCESS用户密码
     * @param $access_charset_var - ACCESS连接编码方式
     */
    function __construct() {
        global $access_db, $access_user, $access_pwd, $access_charset;
        $this->access_db = $access_db;
        $this->access_user = $access_user;
        $this->access_pwd = $access_pwd;
        $this->access_charset = $access_charset;
        $this->access_array = array();
    }

    public function Open() {
        $this->access_str = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" . realpath($this->access_db);
        $this->access_link = new COM("ADODB.Connection");
        $this->access_link->Open($this->access_str); //or die("ado error:".$this->access_link->ErrorMsg()); 
    }

    public function Close() {
        $this->access_link->Close;
    }

    public function Sql($sql) {
        $this->access_rs = new COM("ADODB.RecordSet");
        $re_sql = iconv($this->access_charset, "gb2312", $sql);
        $this->access_rs->Open($re_sql, $this->access_link);
        $this->field_count = $this->access_rs->Fields->Count;
        $i = 0;
        while (!$this->access_rs->Eof) {
            $i++;
            $arr = array();
            for ($j = 0; $j < $this->field_count; $j++) {
                $field_name = $this->access_rs->Fields[$j]->Name;
                $field_value = $this->access_rs->Fields[$j]->Value;
                $arr[$field_name] = $field_value;
            }
            $this->access_array[$i] = $arr;
            $this->access_rs->MoveNext();
        }
        return $this->access_array;
    }

    public function Rows() {
        $this->access_rows = $this->access_rs->RecordCount();
        return $this->access_rows;
    }

    /**
     * get_result取结果集的值
     * @param $field - 要得到值的字段名或字段编号
     * @return 返回取得的值
     */
    public function Get($field, $row) {
        $result = $this->access_array[$row][$field];
        return $result;
    }

    public function Next_row() {
        $this->access_rs->MoveNext();
    }

    function __destruct() {
        unset($this->access_db);
        unset($this->access_user);
        unset($this->access_pwd);
        unset($this->access_str);
        unset($this->access_link);
        unset($this->access_rs);
        unset($this->access_charset);
        unset($this->access_rows);
        unset($this->access_array);
        unset($this->field_count);
    }

}

?>
